package com.csst.business.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.csst.business.model.TMessageBoard;
import com.csst.business.model.TTrUser;
import com.csst.business.model.TWuser;
import com.csst.business.service.IMessageBoardService;
import com.csst.business.service.TestService;
import com.csst.business.util.PageData;
import com.csst.business.util.PaginationUtil;
import com.csst.popular.bean.User;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class MessageBoardAction extends ActionSupport{
	private IMessageBoardService  messageBoardService;
	private TMessageBoard messageBoard;
	private List<TMessageBoard> messagList;
	private Map<TMessageBoard,List<TMessageBoard>> messageMap;
	private int start = 0;
	private static int psize=10;
	private boolean flag;
	//分页参数
	private PageData pageData;
	private int pagesize=10;
	private  int currentPage=1;
	
	public String testAction(){
		System.out.println("=============================");
		return "bbb";
	}
	//删除消息，判断是否有子消息
	public boolean haveChildDateAjax(String id){
		flag = messageBoardService.haveChildDate(id);
		return flag;
	}
	//
	public boolean deleteDateAjax(String id){
		flag = messageBoardService.deleteDate(id);
		return flag;
	}
	//获取留言板前十条数据（微信端）
	public String getWmessageList(){
		HttpServletRequest request = ServletActionContext.getRequest();
		TWuser tuser = (TWuser) request.getSession().getAttribute("tuser");
		if(tuser!=null){
			messageMap = new LinkedHashMap<TMessageBoard,List<TMessageBoard>>();
			List<TMessageBoard> mList = messageBoardService.getMessageList(0,10);
			if(mList.size()>0){
				for(int i=0;i<mList.size();i++){
					TMessageBoard messageb = (TMessageBoard)mList.get(i);
					if(messageb.getFkMId().equals("0")){
						String msid = messageb.getId();
						List<TMessageBoard> childlist =messageBoardService.getChildMessageByFkId(msid); 
						messageMap.put(messageb, childlist);
					}
				}
			}
			return "messageBoard";
		}else{
			return "error";
		}
		
	}
	//获取留言板数据（后台、分页）
	public String getMoredMessageList(){
		messageMap = new LinkedHashMap<TMessageBoard,List<TMessageBoard>>();
		//获取总条数
		int totle = messageBoardService.getMessageTotle();
		System.out.println(currentPage);
		pageData= PaginationUtil.getPageData(pagesize,totle, currentPage);
		List<TMessageBoard> mList = messageBoardService.getMessageList(pageData.getStartNum(),pageData.getEndNum());
		if(mList.size()>0){
			for(int i=0;i<mList.size();i++){
				TMessageBoard messageb = (TMessageBoard)mList.get(i);
				if(messageb.getFkMId().equals("0")){
					String msid = messageb.getId();
					List<TMessageBoard> childlist =messageBoardService.getChildMessageByFkId(msid); 
					messageMap.put(messageb, childlist);
				}
			}
		}
		return "messageB";
	}
	//获取留言板后十条数据（微信端）
	public String getMoredWmessageList(){
		HttpServletRequest request = ServletActionContext.getRequest();
		TWuser tuser = (TWuser) request.getSession().getAttribute("tuser");
		if(tuser!=null){
			messageMap = new LinkedHashMap<TMessageBoard,List<TMessageBoard>>();
			psize = psize +10;
			List<TMessageBoard> mList = messageBoardService.getMessageList(0,psize);
			if(mList.size()>0){
				for(int i=0;i<mList.size();i++){
					TMessageBoard messageb = (TMessageBoard)mList.get(i);
					if(messageb.getFkMId().equals("0")){
						String msid = messageb.getId();
						List<TMessageBoard> childlist =messageBoardService.getChildMessageByFkId(msid); 
						messageMap.put(messageb, childlist);
					}
				}
			}
			return "messageBoard";
		}else{
			return "error";
		}
		
	}
	//保存消息
	public void saveMessage(){
		messageBoard.setMDate(new Date());
		messageBoardService.saveMessage(messageBoard);
		try {
			ServletActionContext.getResponse().getWriter().print(true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public IMessageBoardService getMessageBoardService() {
		return messageBoardService;
	}

	public void setMessageBoardService(IMessageBoardService messageBoardService) {
		this.messageBoardService = messageBoardService;
	}

	public TMessageBoard getMessageBoard() {
		return messageBoard;
	}

	public void setMessageBoard(TMessageBoard messageBoard) {
		this.messageBoard = messageBoard;
	}


	public List<TMessageBoard> getMessagList() {
		return messagList;
	}

	public void setMessagList(List<TMessageBoard> messagList) {
		this.messagList = messagList;
	}

	public Map<TMessageBoard, List<TMessageBoard>> getMessageMap() {
		return messageMap;
	}

	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	public void setMessageMap(Map<TMessageBoard, List<TMessageBoard>> messageMap) {
		this.messageMap = messageMap;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getPsize() {
		return psize;
	}

	public void setPsize(int psize) {
		this.psize = psize;
	}
	public PageData getPageData() {
		return pageData;
	}
	public void setPageData(PageData pageData) {
		this.pageData = pageData;
	}
	public int getPagesize() {
		return pagesize;
	}
	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	
	
}
